package org.softmed.rest.server.test.creators;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilderFactory;

import org.restlet.data.Response;
import org.w3c.dom.Document;

public abstract class AbstractTestCaseCreator {

	protected DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
			.newInstance();
	protected AbstractTestScenarioCreator creator;
	protected Document doc;

	public String testCaseProxy(String uri, String elementURI) throws Throwable {

		return testCase(uri, elementURI);
	}

	public abstract String testCase(String uri, String elementURI)
			throws Throwable;

	public AbstractTestScenarioCreator getCreator() {
		return creator;
	}

	public void setCreator(AbstractTestScenarioCreator creator) {
		this.creator = creator;

	}

	// public String toString(Document doc) throws Throwable {
	// StringWriter sw = new StringWriter();
	// Transformer serializer = TransformerFactory.newInstance()
	// .newTransformer();
	// serializer.transform(new DOMSource(doc), new StreamResult(sw));
	// return sw.toString();
	//
	// }

	public void setupXMLDoc() throws Throwable {
		String mimeType = creator.getTestCase().getResponse().getMimeType();
		if (mimeType !=null && mimeType.equals("text/html"))
			return;

		String xml = creator.getTestCase().getResponse().getBody();
		doc = documentBuilderFactory.newDocumentBuilder().parse(
				new ByteArrayInputStream(("<?xml version=\"1.0\" ?>" + xml)
						.getBytes()));
	}
	//
	// public void setupTEXTDoc(Response response) throws Throwable {
	// String ent = response.getEntity().getText();
	// creator.getTestCase().setBody(ent);
	// creator.getTestCase().setStatus(response.getStatus().getName());
	// }
}
